package com.jinlejia.feedback.idea.ui.adapter;

import androidx.databinding.DataBindingUtil;
import android.graphics.Color;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.freedom.yefeng.yfrecyclerview.YfListAdapter;
import com.freedom.yefeng.yfrecyclerview.YfSimpleViewHolder;
import com.jinlejia.feedback.R;
import com.jinlejia.feedback.databinding.FbItemToquesionBinding;

import java.util.ArrayList;
import java.util.Map;

/**
 * @author SongJiaTai
 * @description:意见反馈列表适配
 * @date :2019/6/26 17:25
 */
public class FbFeedbackAdapter extends YfListAdapter<Map<String, Object>>{


    public FbFeedbackAdapter(ArrayList<Map<String, Object>> data) {
        super(data);
    }

    @Override
    public RecyclerView.ViewHolder onCreateDataViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fb_item_toquesion, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateEmptyViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fb_empty_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateLoadingViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fb_loading_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateErrorViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fb_error_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public void onBindDataViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof  ViewHolder){
            bindCurrViewHolder((ViewHolder) holder,position);
        }
    }

    private void bindCurrViewHolder(ViewHolder holder, int positon){
        FbItemToquesionBinding binding = DataBindingUtil.getBinding(holder.itemView);
        final Map<String, Object> map = mData.get(positon);
        binding.tvDate.setText(map.get("askTime").toString());
        int is = Integer.parseInt(map.get("isGeneral").toString());
        int state = Integer.parseInt(map.get("state").toString());
        String problemTitle = String.valueOf(map.get("problemTitle"));
        if (is==1){
            problemTitle = "       "+problemTitle;
            binding.tvTitle.setText(problemTitle);
            binding.tvZong.setVisibility(View.VISIBLE);
        }else{
            binding.tvZong.setVisibility(View.GONE);
            binding.tvTitle.setText(problemTitle);
        }

        if (state==0){
            binding.tvTag.setText("未受理");
            binding.tvTag.setTextColor(Color.parseColor("#999999"));
        }else if(state==1){
            binding.tvTag.setText("已受理");
            binding.tvTag.setTextColor(Color.parseColor("#999999"));
        }else if(state==2){
            binding.tvTag.setText("解决中");
            binding.tvTag.setTextColor(Color.parseColor("#FCCB00"));
        }else if(state==3){
            binding.tvTag.setText("已完成");
            binding.tvTag.setTextColor(Color.parseColor("#999999"));
        }else if(state==4){
            binding.tvTag.setText("已解决");
            binding.tvTag.setTextColor(Color.parseColor("#999999"));
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (eventitem!=null){
                    eventitem.itemClick(String.valueOf(map.get("problemCode")));
                }
            }
        });
    }

    public interface  Eventitem{
        void itemClick(String problemCode);
    }

    private Eventitem eventitem;

    public void setEventitem(Eventitem eventitem) {
        this.eventitem = eventitem;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        public ViewHolder(View itemView) {
            super(itemView);
            DataBindingUtil.bind(itemView);
        }
    }


}
